import { getNanoid } from '@/utils';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createHash } from 'crypto';
import { ShuwenService } from './shuwen.service';
import { ChainTypeEnum } from '@/enums';
import { ConfluxService } from './conflux.service';

@Injectable()
export class ChainService {
    private readonly chain: ChainTypeEnum;
    constructor(
        private readonly config: ConfigService,
        private readonly shuwen: ShuwenService,
        private readonly conflux: ConfluxService,
    ) {
        this.chain = this.config.get('blockchain');
    }

    public async createHash(): Promise<string> {
        const id = await getNanoid(32);
        return new Promise((r) =>
            r('0x' + createHash('sha256').update(id).digest('hex')),
        );
    }

    public async createAccount(id?: string): Promise<{
        address: string;
        publicKey: string;
    }> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.createAccount(id);
        }
        if (this.chain === ChainTypeEnum.CONFLUX) {
            return await this.conflux.createAccount(id);
        }
    }

    public async classesDeploy(
        data: any = {},
        headers: any = {},
    ): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.classesDeploy(data, headers);
        }
    }

    public async classesDetail(
        data: any = {},
        headers: any = {},
    ): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.classesDetail(data, headers);
        }
    }

    public async nftMint(data: any = {}, headers: any = {}): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.nftMint(data, headers);
        }
    }

    public async nftBurn(data: any = {}, headers: any = {}): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.nftBurn(data, headers);
        }
    }

    public async nftDetail(data: any = {}, headers: any = {}): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.nftDetail(data, headers);
        }
    }

    public async nftTransfer(data: any = {}, headers: any = {}): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.nftTransfer(data, headers);
        }
    }

    public async nftTx(data: any = {}, headers: any = {}): Promise<any> {
        if (this.chain === ChainTypeEnum.SHUWEN) {
            return await this.shuwen.nftTx(data, headers);
        }
    }

    public async generateOperationId() {
        return await getNanoid(32);
    }
}
